/**
 * 防抖函数
 * @param fn
 * @param wait
 * @returns {function(...[*]=)}
 */
let timeout = null;
function debounce(fn, wait) {
    return function() {
        let args = arguments;
        if(timeout !== null) clearTimeout(timeout)
        timeout = setTimeout(function () {
            fn.apply(null, args);
            timeout = null;
        }, wait);
    }
}

/**
 * 节流函数
 * @param fn
 * @param delay
 * @returns {function(...[*]=)}
 */
function throttle(fn, delay) {
    let prev = Date.now();
    return function() {
        let now = Date.now()
        let args = arguments;
        if (now - prev > delay) {
            fn.apply(null, args);
            prev = Date.now();
        }
    }
}

export {
    debounce,
    throttle
}